// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    baseURL: wx.baseURL,
    newsTimer: 0,
    newsIndex: 0,
    banners: [],
    news: [],
    Icon: [],
    timer: 0,
    lists: [],
    newItems: 'transform: translate(0rpx, 0rpx);transition: transform 0.3s;',
    pageNum: 1,
    total: 0,
    loading: '数据加载中...'
  },
  click: function (e) {
    console.log(e)
    var name = e.currentTarget.dataset.name;
    console.log('点击姓名' + name)
  },
  // 新闻
  getNews() {
    return new Promise((resolve, reject) => {
      wx.get('/index/news').then((res) => {
        this.setData({
          news: res
        })
        this.changeNewIndex() //触发新闻轮播
        resolve()
      })
    })
  },


  // 新闻轮播
  changeNewIndex() {
    if (this.data.newsTimer) {
      clearInterval(this.data.newsTimer)
      this.data.newsTimer = 0
    }
    this.data.newsTimer = setInterval(() => {
      let newsIndex = this.data.newsIndex
      newsIndex++
      //if (newsIndex == this.data.news.length) newsIndex = 0 //最后一张图切换到第一张图
      //console.log('这是第几张', newsIndex)
      this.setData({
        newsIndex,
        newItems: 'transform: translate(0rpx, -' + newsIndex * 80 + 'rpx);transition: transform 0.3s;'
      })
      if (this.data.newsIndex == this.data.lists.length) {
        setTimeout(() => {
          this.setData({
            newsIndex: 0,
            newItems: 'transform: translate(0rpx, 0rpx);'
          })
        }, 500)
      }
    }, 3000)
  },
  //下拉刷新
  onPullDownRefresh() {
    this.data.pageNum = 1
    this.data.lists = []
    this.getList()
  },
  // 轮播图
  getBanners() {
    return new Promise((resolve, reject) => {
      wx.get('/index/banners').then(res => {
        this.setData({
          banners: res
        })
        resolve()
      })
    })
  },
  // 轮播图跳转事件
  swiperClick() {
    let path = this.data.banners.path
    wx.switchTab({
      url: this.path,
    })

  },
  // 获取分类
  getIcon() {
    return new Promise((resolve, reject) => {
      wx.get('/index/categorys').then((res) => {
        this.setData({
          Icon: res
        })
        resolve()
      })
    })
  },

  onReachBottom() {
    if (this.data.total > this.data.lists.length) {
      this.data.pageNum++
      this.getRecomend()
    }
  },

  // 获取推荐
  getRecomend() {
    return new Promise((resolve, reject) => {
      this.setData({
        loading: '数据加载中...'
      })
      wx.get('/index/recommends', {
        pageNum: this.data.pageNum
      }).then(res => {
        this.setData({
          loading: '小主，已经到底了哦~',
          lists: this.data.lists.concat(res.rows),
          total: res.total
        })
        resolve()
      })
    })
  },

  getList() {
    Promise.all([this.getNews(), this.getBanners(), this.getIcon(), this.getRecomend()]).then(() => {
      console.log('所有数据加载完成')
    }).catch(e => {
      console.log(e)
    })
  },

  onLoad() {
    this.getList()
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})